﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MMK.Paket_Tracker.Runtime.ValidationRules
{
    /// <summary>
    /// Adress validation
    /// </summary>
    public static class AdressValidator
    {
        /// <summary>
        /// Validates the specified value.
        /// </summary>
        /// <param name="name">The name of the value.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static String Validate(String name, String value)
        {
            String result = null;

            switch (name)
            {
                case Fields.FIRST_NAME:
                    result = CheckLength(value, 3, 50);
                    break;
                case Fields.LAST_NAME:
                    result = CheckLength(value, 3, 50);
                    break;
                case Fields.TOWN:
                    result = CheckLength(value, 3, 50);
                    break;
                case Fields.STREET:
                    result = CheckLength(value, 3, 20);
                    break;
                case Fields.NUMBER:
                    result = CheckLength(value, 1, 10);
                    break;
                default:
                    break;
            }

            return result;
        }

        /// <summary>
        /// Validates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static String Validate(String name, int value)
        {
            String result = null;

            result = CheckPLZ(value, 10000, 19999);

            return result;
        }

        private static String CheckLength(String value, Int32 min, Int32 max)
        {
            String result = null;

            if (value == null)
            {
                result = "Es wurde noch kein Text eingegeben!";
            }
            else
            {
                if (value.Length < min)
                {
                    result = "Der eingegebene Text ist zu kurz!";
                }

                if (value.Length > max)
                {
                    result = "Der eingegebene Text ist zu lang!";
                }

            }
            return result;
        }

        private static String CheckPLZ(int value, int min, int max)
        {
            String result = null;

            if (value == 0)
            {
                result = "Es wurde noch keine Postleitzahl eingegeben!";
            }
            else
            {
                if (value < min)
                {
                    result = "Die Postleitzahl ist zu klein!";
                }

                if (value > max)
                {
                    result = "Die Postleitzahl ist zu groß!";
                }
            }
            return result;
        }
    }
}
